package com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver;

import android.text.TextUtils;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DNManager;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DNManager.ResolveTriggerType;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DNManager.ResolverSource;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsUtil;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.cache.CacheManager;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.dnkeeper.DomainResult;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.log.DnsEventListener;

public abstract class DNResolver implements Runnable {
    private static final String TAG = "DNResolver";
    private DNResolverCallback dnResolverCallback;
    private DnsEventListener dnsEventListener;
    private DomainResult dnsResult;
    protected final String domain;
    @ResolverSource
    protected final int source;
    @ResolveTriggerType
    private String triggerType;

    public interface DNResolverCallback {
        void onFailure(String str, Throwable th);

        void onRespone(String str, DomainResult domainResult);
    }

    public static final class DefaultDNResolverCallback implements DNResolverCallback {
        public void onRespone(String str, DomainResult domainResult) {
            CacheManager.saveValidIP(str, domainResult);
        }

        public void onFailure(String str, Throwable th) {
        }
    }

    abstract DomainResult query();

    DNResolver(String str, @ResolverSource int i, @ResolveTriggerType String str2) {
        this.domain = str;
        this.source = i;
        this.triggerType = str2;
        this.dnResolverCallback = null;
        this.dnsEventListener = DNManager.getInstance().getEventListenerFactory().create(this);
    }

    DNResolver(String str, @ResolverSource int i, @ResolveTriggerType String str2, DNResolverCallback dNResolverCallback) {
        this.domain = str;
        this.source = i;
        this.triggerType = str2;
        this.dnsEventListener = DNManager.getInstance().getEventListenerFactory().create(this);
        this.dnResolverCallback = dNResolverCallback;
    }

    public void run() {
        this.dnsEventListener.resolveStart();
        if (TextUtils.isEmpty(this.domain)) {
            this.dnsEventListener.resolveFail(new Exception("domain == null"));
            return;
        }
        set(query());
        if (DnsUtil.isIpListEmpty(this.dnsResult)) {
            Logger.i(TAG, "query failed, dnsResult is null, domain:" + this.domain);
            Throwable exception = new Exception("query failed, dnsResult is null, domain:" + this.domain);
            this.dnsEventListener.resolveFail(exception);
            if (this.dnResolverCallback != null) {
                this.dnResolverCallback.onFailure(this.domain, exception);
                return;
            }
            return;
        }
        this.dnsEventListener.resolveEnd(this.dnsResult);
        if (this.dnResolverCallback != null) {
            this.dnResolverCallback.onRespone(this.domain, this.dnsResult);
        }
    }

    void set(DomainResult domainResult) {
        this.dnsResult = domainResult;
    }

    DomainResult get() {
        return this.dnsResult;
    }

    public String getTriggerType() {
        return this.triggerType;
    }
}
